using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using System.Configuration;

namespace myGCalendar
{
    public partial class CalendarForm : Form
    {
        private ResourceManager rm = new ResourceManager("myGCalendar.myRes", System.Reflection.Assembly.GetExecutingAssembly());
        private Screen screen = Screen.PrimaryScreen;
        private CalConnection conn = null;
        private ArrayList allEntries = null;
        EventPanel panel = null;

        public CalendarForm()
        {
            InitializeComponent();

            this.notifyIcon1.Icon = (System.Drawing.Icon)rm.GetObject("favicon");
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
            System.Diagnostics.Process.Start("http://www.google.com/calendar");
        }
       
        private void Form1_Load(object sender, EventArgs e) {
            conn = new CalConnection();

            if (!conn.login(this)) {
                this.Dispose();
                return;
            }

            checkIntervall.Enabled = true;

            allEntries = conn.getAllEvents(new DateTime());

            panel = new EventPanel(allEntries);
            this.flowLayoutPanel1.Controls.Add(panel);
            /*
            foreach (CalEvent entry in allEntries) {
                Button btn = new Button();
                                
                if (!entry.AllDay)
                    btn.Text = entry.StartTime.ToShortTimeString() + " ";
                btn.Text += entry.Title;
                btn.BackColor = entry.Color;
                btn.ForeColor = Color.White;
                btn.AutoSize = true;
                btn.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                
                this.flowLayoutPanel1.Controls.Add(btn);
            }
                */
            this.Location = new Point(screen.WorkingArea.Width-this.Width, screen.WorkingArea.Height-this.Height);
       
            timer1.Enabled = true;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            this.notifyIcon1 = null;
            this.Dispose();
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Show();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e) {
            this.Hide();
            timer1.Enabled = false;
        }

        private void viewCalendarToolStripMenuItem_Click(object sender, EventArgs e) {
            System.Diagnostics.Process.Start("http://www.google.com/calendar");
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) {
            if (this.Visible && e.Button == MouseButtons.Left) {
                this.Hide();
                timer1.Enabled = false;
            } else if (e.Button == MouseButtons.Left) {
                this.Show();
                this.BringToFront();
                this.Activate();
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e) {
            
        }

        private void checkIntervall_Tick(object sender, EventArgs e) {
            panel.checkActive();
        }
    }
}